package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.data.DataBase;
import com.entity.User;

@WebServlet(urlPatterns={"/login"})
public class LoginServlet extends HttpServlet{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		
		PrintWriter out = resp.getWriter();
		
		//获得参数
		String username=req.getParameter("username");
		String password=req.getParameter("password");
		if(DataBase.data.containsKey(username)){
			//用户名没有注册
			User user = DataBase.data.get(username);
			if(user.getPassword().equals(password)){
				out.print("1");
				req.getSession().setAttribute("user", user);
				return;
			}
		}
		//登录失败
		out.print("0");
	}
}
